package com.zenjava.zendeploy.testapp;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import zom.zenjava.zendeploy.gui.jfx.ApplicationUpdateWizard;
import zom.zenjava.zendeploy.gui.jfx.CurrentVersionBar;

public class ZenDeployTestApp extends Application
{
    public static void main(String[] args)
    {
        Application.launch(args);
    }

    private StackPane root;
    private TextField nameField;
    private Label helloMessageLabel;
    private ApplicationUpdateWizard applicationUpdateWizard;

    public void start(Stage stage)
    {
        stage.setTitle("Test Application");
        Scene scene = new Scene(buildView(), 500, 400);
        scene.getStylesheets().add("zen-deploy-styles.css");
        scene.getStylesheets().add("style.css");
        stage.setScene(scene);
        stage.show();
    }
    
    protected Parent buildView()
    {
        BorderPane contentPane = new BorderPane();
        contentPane.setTop(buildInputArea());
        contentPane.setCenter(buildMessageArea());
        contentPane.setBottom(buildUpdateOptionsArea());

        root = new StackPane();
        root.getChildren().add(contentPane);
        return root;
    }

    private Node buildInputArea()
    {
        HBox box = new HBox(10);
        box.getStyleClass().add("header");

        EventHandler<ActionEvent> sayHelloAction = new EventHandler<ActionEvent>()
        {
            public void handle(ActionEvent event)
            {
                String name = nameField.getText();
                name = name != null && name.trim().length() > 0 ? name : "stranger";
                helloMessageLabel.setText(String.format("Hello %s!", name));
            }
        };

        Label prompt = new Label("Your name ");
        box.getChildren().add(prompt);
        
        nameField = new TextField();
        nameField.setPromptText("enter your name");
        nameField.setOnAction(sayHelloAction);
        box.getChildren().add(nameField);
        
        Button sayHelloButton = new Button();
        sayHelloButton.setText("Say Hello");
        sayHelloButton.setOnAction(sayHelloAction);
        box.getChildren().add(sayHelloButton);
        
        return box;
    }

    private Node buildMessageArea()
    {
        helloMessageLabel = new Label();
        helloMessageLabel.getStyleClass().add("hello-message");
        return helloMessageLabel;
    }

    private Node buildUpdateOptionsArea()
    {
        BorderPane pane = new BorderPane();
        pane.getStyleClass().add("footer");

        CurrentVersionBar currentVersionBar = new CurrentVersionBar();
        currentVersionBar.load();
        pane.setLeft(currentVersionBar);

        Button checkForUpdatesButton = new Button("Check for updates");
        checkForUpdatesButton.setOnAction(new EventHandler<ActionEvent>()
        {
            public void handle(ActionEvent event)
            {
                if (applicationUpdateWizard == null)
                {
                    applicationUpdateWizard = new ApplicationUpdateWizard(root);
                }
                applicationUpdateWizard.show();
            }
        });
        pane.setRight(checkForUpdatesButton);
        
        return pane;
    }

}
